<h1>Dugovanja</h1>

<?= $this->Form->create('Filter', array( 'class' => 'filter-form')) ?>

    <?= $this->Form->label('client_id', 'Klijent:') ?>
    <?= $this->Form->select('client_id', $clients, array(
        'empty' => '--SVI--',
        'value' => @$filter['client_id']
    )) ?>
    
    <?= $this->Form->label('start', 'Period:') ?>
    <?= $this->Form->text('start', array(
        'class' => 'datepicker',
        'value' => @$filter['start']
    )) ?>
    -
    <?= $this->Form->text('end', array(
        'class' => 'datepicker',
        'value' => @$filter['end']
    )) ?>

<?= $this->Form->submit('Traži') ?>
<?= $this->Form->end() ?>

<? if (! empty($data)): ?>
<?php
    $total = (object)array(
        'start_due' => 0,
        'invoiced'  => 0,
        'paid'      => 0,
        'due'       => 0
    ); 
?>
<table class="entity-table">
    <tr>
        <th>Klijent</th>
        <th class="text-right" width="10%">Početni dug</th>
        <th class="text-right" width="10%">Fakturisano</th>
        <th class="text-right" width="10%">Uplaćeno</th>
        <th class="text-right" width="10%">Dug</th>
    </tr>
    
    <? foreach ($data as $clientId => $row): ?>
    <?php
        $start_due = $row['start_due'];
        $invoiced  = $row['invoiced'];
        $payed     = $row['payed'];
        $debt      = $invoiced + $start_due - $payed;
        $status    = $debt>0 ? 'error' : 'ok';
        
        $total->start_due += $start_due;
        $total->invoiced  += $invoiced;
        $total->payed     += $payed;
        $total->due       += $debt;
        
        $params = implode("/", array($clientId, $filter['start'], $filter['end']));
    ?>
    <tr class="<?= $status ?>">
        <td><?= $clients[$clientId] ?></td>
        <td align="right"><?= currency_format($start_due) ?></td>
        <td align="right"><?= currency_format($invoiced) ?></td>
        <td align="right"><?= currency_format($payed) ?></td>
        <td align="right">
            <? if($debt): ?>
                <?= $this->Html->link(currency_format($debt), '/reports/debt_details/' . $params, array(
                    'class' => 'details-link'
                )) ?>
            <? else: ?>
                <?= currency_format($debt) ?>
            <? endif ?>
        </td>
    </tr>
    <? endforeach ?>
    <tr class="total">
        <td align="right">UKUPNO:</td>
        <td align="right"><?= currency_format($total->start_due) ?></td>
        <td align="right"><?= currency_format($total->invoiced) ?></td>
        <td align="right"><?= currency_format($total->payed) ?></td>
        <td align="right"><?= currency_format($total->due) ?></td>
    </tr>
</table>
<? endif ?>

<div id="details"></div>
<script type="text/javascript">
$(function(){
    var details = $('#details');
    
    $('.details-link').click(function(){
        var url = this.href + "/ajax:1";
        var clientName = $(this).parent().parent().find('td').eq(0).text()
        
        $.ajax({
            url: url,
            beforeSend: function() {
                details
                    .html('')
                    .addClass('loading')
                    .dialog({
                        title: 'Detalji: ' + clientName,
                        width: 600,
                        height: 400,
                        modal: true
                    })
            },
            complete: function(req) {
                details
                    .removeClass('loading')
                    .html(req.responseText);
            }
        })
        return false;
    });
});
</script>
